<template>
  <span></span>
</template>

<script>
export default {
  props: ["res"],
  data() {
    return {
      fileName: ""
    };
  },
  methods: {
    getExcel(res) {
      require.ensure([], () => {
        const { export_json_to_excel } = require("./Export2Excel.js");
        const tHeader = [
          "当事人",
          "科室",
          "班组",
          "日期",
          "一级分类",
          "二级分类",
          "计分",
          "详情",
          "记录人",
          "检查类型"
        ];
        const filterVal = [
          "clientName",
          "clientOffice",
          "clientGroup",
          "eventTime",
          "category",
          "item",
          "value",
          "description",
          "userName",
          "type"
        ];
        const list = res;
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, this.fileName);
        this.$notify.success({
          title: "已导出文件",
          message: this.fileName + ".xlsx"
        });
        this.$emit("exit");
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]));
    },
    genFileName() {
      let d = new Date();
      let str = "";
      let month = d.getMonth() + 1;
      let date = d.getDate();
      let hour = d.getHours();
      let minute = d.getMinutes();
      let second = d.getSeconds();
      str += d.getFullYear() + ""; 
      if (month < 10) {
        str += "0" + month + "";
      }
      if (month >= 10) {
        str += month + "";
      }
      if (date < 10) {
        str += "0" + date + "_";
      }
      if (date >= 10) {
        str += date + "_";
      }
      if (hour < 10) {
        str += "0" + hour + "";
      }
      if (hour >= 10) {
        str += hour + "";
      }
      if (minute < 10) {
        str += "0" + minute + "";
      }
      if (minute >= 10) {
        str += minute + "";
      }
      if (second < 10) {
        str += "0" + d.getSeconds();
      }
      if (second >= 10) {
        str += d.getSeconds();
      }
      return "纠违记录@" + str;
    }
  },
  mounted() {
    this.fileName = this.genFileName();
    this.getExcel(this.res);
  }
};
</script>